package com.css.ids.server.service.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.css.apps.base.common.BaseEnvironment;
import com.css.apps.base.role.model.SRole;
import com.css.apps.base.user.model.SUser;
import com.css.db.query.QueryCache;
import com.css.util.ListUtil;
import com.css.util.StringHelper;

public class IdsRoleDao {

	public static SRole getRole(String roleId) {
        return QueryCache.get(SRole.class, roleId);
    }
	
	@SuppressWarnings("unchecked")
	public static List<SRole> listRoles(Collection<String> roleIds) {
		return QueryCache.idToObj(SRole.class, new ArrayList<>(roleIds));
	}
	
	@SuppressWarnings("unchecked")
	public static List<String> getRoleIdsByUserId(String userId) {
		SUser user = QueryCache.get(SUser.class, userId);
		if (user == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer(" select distinct a.roleId from SUserRole a where a.userId=:userId");
		return new QueryCache(sb.toString()).setParameter("userId", userId).list();
	}
	
	@SuppressWarnings("unchecked")
	public static List<SRole> getRoleByUserId(String userId) {
		return QueryCache.idToObj(SRole.class, getRoleIdsByUserId(userId));
	}
    
    @SuppressWarnings("unchecked")
	public static List<SRole> getRoleByUserId(String userId, String sysId) {
        SUser user = QueryCache.get(SUser.class, userId);
        if (user == null) {
			return null;
		}
        StringBuffer ur = new StringBuffer(" select distinct a.roleId from SUserRole a where a.userId = :userId and a.sysId = :sysId");
		/*ur.append(" or a.roleId in (select distinct b.roleId from SPostRole b where b.sysId = :sysId and b.postId in");
		ur.append(" (select c.postId from SUserPost c where c.userId = :userId)").append(")");*/
		List<String> roleIds = new QueryCache(ur.toString()).setParameter("userId", userId).setParameter("sysId", sysId).list();
        return roleIds == null ? new ArrayList<>() : QueryCache.idToObj(SRole.class, roleIds);
    }
	
	/**
	 * 查询系统下允许开通的所有角色
	 */
	@SuppressWarnings("unchecked")
	private static List<String> getRoleBySysId(String sysId) {
		if (StringHelper.isEmpty(sysId))
			return null;
		List<String> roleIds = new QueryCache("select a.uuid from SRole a where a.permitDredge = '1' and a.roleType = :roleType and a.sysId = :sysId")
						.setParameter("sysId", sysId).setParameter("roleType", BaseEnvironment.ROLETYPE_NORMAL).list();
		return roleIds;
	}

	@SuppressWarnings("unchecked")
	public static List<SRole> getAllSysRoleBySysId(String sysId) {
		if (StringHelper.isEmpty(sysId))
			return null;
		List<String> roleIds = getRoleBySysId(sysId);
		if (ListUtil.isLstEmpty(roleIds))
			return null;
		return QueryCache.idToObj(SRole.class, roleIds);
	}
	
	@SuppressWarnings("unchecked")
    public static List<SRole> listRolesByFilter(List<String> conditions) {
        if (conditions.isEmpty()) {
            return null;
        }
        StringBuffer sb = new StringBuffer("from SRole where 1=1 ");
        for (String condition : conditions) {
            String[] items = condition.split(",");
            String key = items[0].trim();
            String opt = items[1].trim();
            if (validOperation().contains(opt)) {
                sb.append(" and ").append(key).append(" ").append(opt).append(" :").append(key);
            }
        }
        QueryCache qc = new QueryCache(sb.toString());
        for (String condition : conditions) {
            String[] items = condition.split(",");
            String val = items[2].trim();
            String opt = items[1].trim();
            String key = items[0].trim();
            if (validOperation().contains(opt)) {
                if ("like".equals(opt)) {
                    qc.setParameter(key, "%" + val + "%");
                } else {
                    qc.setParameter(key, val);
                }
            }
        }
        return qc.list();
    }

    private static List<String> validOperation() {
        List<String> list = new ArrayList<>();
        list.add("=");
        list.add("!=");
        list.add(">");
        list.add(">=");
        list.add("<=");
        list.add("like");
        return list;
    }

}
